package sskj.regulatory.presentation.screen.registerMakePas

import android.animation.ObjectAnimator
import android.annotation.SuppressLint
import android.text.*
import android.text.style.AbsoluteSizeSpan
import android.view.View
import android.view.animation.LinearInterpolator
import com.qmuiteam.qmui.widget.dialog.QMUIDialog
import com.qmuiteam.qmui.widget.dialog.QMUITipDialog
import com.vondear.rxtools.RxKeyboardTool
import kotlinx.android.synthetic.main.fragment_login.*
import kotlinx.android.synthetic.main.fragment_login.view.*
import kotlinx.android.synthetic.main.fragment_register_makepas.*
import kotlinx.android.synthetic.main.fragment_register_makepas.view.*
import sskj.regulatory.R
import sskj.regulatory.R.id.etRepeatPassword
import sskj.regulatory.R.id.etSetPassword
import sskj.regulatory.domain.entity.user.UserEntity
import sskj.regulatory.presentation.base_mvp.base.BaseFragment
import sskj.regulatory.presentation.screen.login.LoginFragment
import sskj.regulatory.presentation.screen.registeCaptcha.RegisterCaptchaFragment
import sskj.regulatory.presentation.screen.registeComplete.RegisteCompleteFragment
import sskj.regulatory.presentation.screen.register.*
import sskj.regulatory.presentation.utils.delegates.bundle
import sskj.regulatory.ribble.presentation.utils.extensions.text
import javax.inject.Inject

class RegisteMakePasFragment : BaseFragment<RegisterMakePasContract.View, RegisterMakePasContract.Presenter>(), RegisterMakePasContract.View {

    private val userEntity: UserEntity by bundle()

    @Inject
    lateinit var mPresenter: RegisterMakePasPresenter

    override fun injectDependencies() = activityComponent.inject(this)

    override fun initPresenter() = mPresenter

    override val layoutResId: Int = R.layout.fragment_register_makepas

    override fun onBackPressed(): Boolean {
        baseFragmentActivity.popBackStack(LoginFragment::class.java)
        return false
    }

    override fun initView() {
        initEvent()
        initTopBar()
        initHitView()
    }

    private fun initTopBar() {
        mCacheView.rmTopBar.addLeftBackImageButton().setOnClickListener { _ ->
            baseFragmentActivity.popBackStack(LoginFragment::class.java)
        }
    }

    @SuppressLint("ResourceAsColor")
    private fun initHitView() {
        var ss: SpannableString = SpannableString("请输入密码")
        val ass: AbsoluteSizeSpan = AbsoluteSizeSpan(14, true)

        ss.setSpan(ass, 0, ss.length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        mCacheView.etSetPassword.hint = SpannedString(ss);
        mCacheView.etSetPassword.setHintTextColor(R.color.qmui_config_color_50_pure_black)

        ss = SpannableString("请重复密码")
        ss.setSpan(ass, 0, ss.length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

        mCacheView.etRepeatPassword.hint = SpannedString(ss);
        mCacheView.etRepeatPassword.setHintTextColor(R.color.qmui_config_color_50_pure_black)

        mCacheView.messageTv.visibility = View.GONE

        RxKeyboardTool.showSoftInput(context, mCacheView.etSetPassword)

    }

    override fun showError(message: String?) {
        QMUIDialog.MessageDialogBuilder(getActivity())
                .setTitle("错误提示")
                .setMessage(message)
                .addAction("确定") { dialog, _ -> dialog.dismiss() }
                .create(R.style.QMUI_Dialog).show()
    }

    private fun initEvent() {

        mCacheView.etRepeatPassword.addTextChangedListener(object : TextWatcher {
            override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {

            }

            override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {

                when (mCacheView.etSetPassword.text.isNotEmpty() && s.toString().length >= mCacheView.etSetPassword.text.toString().length) {
                    true -> {
                        if (mCacheView.etSetPassword.text.toString() == s.toString()) {
                            mCacheView.messageTv.visibility = View.GONE
                            mCacheView.setPasButton.isEnabled = true
                        } else {
                            mCacheView.messageTv.visibility = View.VISIBLE
                            mCacheView.setPasButton.isEnabled = false
                        }
                    }
                    false -> {
                        mCacheView.messageTv.visibility = View.GONE
                    }
                }


            }

            override fun afterTextChanged(s: Editable) {
                if (!TextUtils.isEmpty(s) && mCacheView.cleanRepeatPassword.visibility == View.GONE) {
                    mCacheView.cleanRepeatPassword.visibility = View.VISIBLE
                } else if (TextUtils.isEmpty(s)) {
                    mCacheView.cleanRepeatPassword.visibility = View.GONE
                }
            }
        })

        mCacheView.etSetPassword.addTextChangedListener(object : TextWatcher {
            override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {

            }

            override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {
                when (mCacheView.etRepeatPassword.text.isNotEmpty() && s.toString().length >= mCacheView.etRepeatPassword.text.toString().length) {
                    true -> {
                        if (mCacheView.etSetPassword.text.toString() == s.toString()) {
                            mCacheView.messageTv.visibility = View.GONE
                            mCacheView.setPasButton.isEnabled = true
                        } else {
                            mCacheView.messageTv.visibility = View.VISIBLE
                            mCacheView.setPasButton.isEnabled = false
                        }
                    }
                    false -> {
                        mCacheView.messageTv.visibility = View.GONE
                    }
                }
            }

            override fun afterTextChanged(s: Editable) {
                if (!TextUtils.isEmpty(s) && mCacheView.cleanSetPassword.visibility == View.GONE) {
                    mCacheView.cleanSetPassword.visibility = View.VISIBLE
                } else if (TextUtils.isEmpty(s)) {
                    mCacheView.cleanSetPassword.visibility = View.GONE
                }
            }
        })
        mCacheView.cleanSetPassword.setOnClickListener { etSetPassword.setText("") }
        mCacheView.cleanRepeatPassword.setOnClickListener { etRepeatPassword.setText("") }
        mCacheView.setPasButton.setOnClickListener {
            userEntity.password = etSetPassword.text.toString()
            mPresenter.register(userEntity)
        }
    }

    override fun registerOk() {
        var registeCompleteFragment = RegisteCompleteFragment()
        registeCompleteFragment.arguments = RegisterFragment.getBundle(userEntity)
        startFragment(registeCompleteFragment)
    }

    override fun registerError(message: String?) {
        QMUIDialog.MessageDialogBuilder(getActivity())
                .setTitle("错误提示")
                .setMessage("注册失败,错误原因: ${message}。")
                .addAction("确定") { dialog, _ -> dialog.dismiss() }
                .create(R.style.QMUI_Dialog).show()
    }

    override fun showLoading() {
        activity.tipDialog = QMUITipDialog.Builder(context)
                .setIconType(QMUITipDialog.Builder.ICON_TYPE_LOADING)
                .setTipWord("加载中")
                .create()
        activity.tipDialog?.show()
    }

    override fun hideLoading() {
        activity.tipDialog?.dismiss()
    }

}